define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/enter/index' + location.search,
                    // add_url: 'purchase/enter/add',
                    // edit_url: 'purchase/enter/edit',
                    // del_url: 'purchase/enter/del',
                    // multi_url: 'purchase/enter/multi',
                    table: 'purchase_enter',
                }
            });
            $('.btn-export').click(function () {
                var url = Config.this_url+'/purchase/enter/export'
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['30%', '70%'],
                    title: "导出选择",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            })



            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 4) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'enter_id'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'purchaseorder.number', title: __('purchaseorder.Number')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        // {field: 'abstract', title: __('Abstract')},
                        {field: 'paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        // {field: 'purchaseorder.number', title: __('Purchaseorder.number')},
                        {field: 'supplier.nickname', title: __('Supplier.nickname'),operate:'LIKE'},
                        {field: 'storehouse.name', title: __('Storehouse.name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=3")},
                        {
                            field: 'goods_name', title: __('商品名称'), visible: false
                        },
                        {
                            field: 'goods_number', title: __('商品编号'), visible: false
                        },
                        {field: 'explain', title: __('Explain')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'examine',
                                    title: __('采购单审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var enter_id =$(this).parents('tr').find('.enter_id').html()
                                        var url = Config.this_url+'/purchase/enter/detail?ids='+enter_id
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "采购详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                        // // var url = Config.this_url+'/purchase/order/enter?ids='+oid
                                        // layer.confirm('是否通过该采购单？', {
                                        //     btn: ['确定','驳回'] //按钮
                                        // }, function(){
                                        //     var url = Config.this_url+'/purchase/enter/examine?ids='+enter_id
                                        //     $.post(url,{status:2},function (res) {
                                        //         layer.msg(res.msg)
                                        //         if(res.code == 1){
                                        //             $('.btn-refresh').click()
                                        //         }
                                        //     })
                                        // }, function(){
                                        //     var url = Config.this_url+'/purchase/enter/examine?ids='+enter_id
                                        //     $.post(url,{status:3},function (res) {
                                        //         layer.msg(res.msg)
                                        //         if(res.code == 1){
                                        //             $('.btn-refresh').click()
                                        //         }
                                        //     })
                                        // });

                                    }},
                                {
                                    name: 'retreat',
                                    title: __('退货'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-level-up',
                                    hidden:function(row){
                                        if(row.ystatus != 2){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var enter_id =$(this).parents('tr').find('.enter_id').html()
                                        var url = Config.this_url+'/purchase/enter/retreat?ids='+enter_id
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "退货",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'share',
                                    title: __('平摊成本'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa  fa-expand',
                                    hidden:function(row){
                                        if(row.is_share == 1 || row.ystatus != 2){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var enter_id =$(this).parents('tr').find('.enter_id').html()
                                        var url = Config.this_url+'/purchase/enter/share?ids='+enter_id
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "平摊成本",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'printing',
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-warning btn-click editstatus',
                                    icon: 'fa fa-print',
                                    click: function(data){
                                        var enter_id =$(this).parents('tr').find('.enter_id').html()
                                        window.open(Config.this_url+'/purchase/enter/printing?ids='+enter_id)
                                    }},
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var enter_id = $(this).parents('tr').find('.enter_id').html()
                var url = Config.this_url+'/purchase/enter/detail?ids='+enter_id
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "入库详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        export: function () {
            $('.btn-embossed').click(function () {

                // console.log($(".checkstatus").val())
                // console.log($("input[ name='is_factory']").val())
                // console.log($("input[ name='end_time']").val())
                // console.log($("input[ name='is_factory']").filter(':checked').val())
                window.location.href = Config.this_url+'/purchase/enter/exportExcel?status='+$(".checkstatus").val()+'&start_time='+$("input[ name='start_time']").val()+'&end_time='+$("input[ name='end_time']").val()+'&supplier_id='+$("#c-supplier_id").val()+'&storehouse_id='+$("#c-storehouse_id").val()+'&is_factory='+$("input[ name='is_factory']").filter(':checked').val()
                return false;
            })
            Controller.api.bindevent();
        },
        share: function () {
            $('.autocount').click(function () {
                    var all_count = $('.price_tax').html()
                    var com_fare = $('#com_fare').val()
                console.log(all_count)
                console.log(com_fare)
                    $('.connnn').find("tr").each(function(){
                        // console.log($(this).find('.count').html())
                        var gcount = $(this).find('.count').html()
                        console.log(gcount)
                            $(this).find('.share').val((com_fare*gcount/all_count).toFixed(2))
                            $(this).find('.share').next().html((com_fare*gcount/all_count).toFixed(2))
                    })
                // count()
            })
            var is_true = 0;
            $('.subpurchase').click(function () {
                if (is_true == 0){
                    layer.confirm('确定分摊吗？', {
                        btn : [ '确定', '取消' ]//按钮
                    }, function(index) {
                        
                        is_true = 1
                        layer.close(index)
                        $('.subpurchase').click()
                        return true
                    });
                    return false;
                }
                is_true = 0;

            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/purchase/enter/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            $('.joint-search').click(function () {
                var url = Config.this_url+'/purchase/order/detail?ids='+$(this).data('id')
                // console.log(url)
                Backend.api.addtabs(url, '销售单详情', 'fa fa-circle-o fa-fw');
            })
            // $('.subpurchase').click(function () {
            //     layer.confirm('审核通过？', {
            //         btn: ['确定','取消'] //按钮
            //     }, function(){
            //         var url = Config.this_url+'/purchase/enter/examine?ids='+$('#enter_id').val()
            //         $.post(url,{status:2},function (res) {
            //             layer.msg(res.msg)
            //             if(res.code == 1){
            //                 parent.$(".btn-refresh").trigger("click");
            //                 var index = parent.Layer.getFrameIndex(window.name);
            //                 parent.Layer.close(index);
            //             }
            //         })
            //     }, function(){
            //
            //     });
            // })
            // $('.reject').click(function () {
            //     layer.confirm('驳回该采购单？', {
            //         btn: ['确定','取消'] //按钮
            //     }, function(){
            //         var url = Config.this_url+'/purchase/enter/examine?ids='+$('#enter_id').val()
            //         $.post(url,{status:3},function (res) {
            //             layer.msg(res.msg)
            //             if(res.code == 1){
            //                 parent.$(".btn-refresh").trigger("click");
            //                 var index = parent.Layer.getFrameIndex(window.name);
            //                 parent.Layer.close(index);
            //             }
            //         })
            //     }, function(){
            //
            //     });
            // })

            Controller.api.bindevent();
        },
        retreat: function () {
            Controller.api.bindevent();
        },
        change: function () {
            $(".btn-add").click(function () {
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: Config.this_url+'/sales/slip/seach',
                })
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});